@extends('admin.admin')

@section('content')
        <!-- Content Header (Page header) -->
<section class="content-header">
    <h1>
        商品库存管理
        <small>商品库存操作日志</small>
    </h1>
    <ol class="breadcrumb">
        <li><a href="/admin"><i class="fa fa-dashboard"></i> 控制面板</a></li>
        <li ><a href="/admin/stock_history">商品库存管理</a></li>
        <li class="active">商品库存操作日志</li>
    </ol>
</section>

<!-- Main content -->
<section class="content">

    <div class="row">
        <div class="col-xs-12">


            <div class="box box-primary ">
                <div class="box-body table-responsive">

                    {!! Form::model(@$goodsStockHistory,array('route' => 'admin.stock_history.index','method' => 'get','class'=>'form-search form-inline pull-right')) !!}


                        <div class="form-group" style="margin-right: 15px;">
                            {!! Form::text('goods_id',$search['goods_id'],['class'=>'form-control','placeholder'=>'商品id'])!!}
                        </div>

                        <div class="form-group" style="margin-right: 15px;">
                            {!! Form::select('stock_status',$stockStatusArr, @$_GET['stock_status'], ['class'=>'form-control','placeholder'=>'操作类型'])!!}
                        </div>

                        <button type="submit" class="btn btn-primary ">搜索</button>
                        <button type="button" onclick="window.location.href='{{route('admin.stock_history.index')}}'" class="btn btn-default">重置</button>

                    {!! Form::close() !!}

                </div>

            </div><!-- end .box box-primary -->


            <div class="box box-primary">
                <div class="box-header">
                    <h3 class="box-title">商品库存操作日志</h3>
                    {{--<div class="box-tools">
                        <a href="{{route('admin.stock.create')}}" type="button" class="btn btn-block btn-success btn-xs"><i class="icon  glyphicon glyphicon-plus"></i> 添加</a>
                    </div>--}}
                </div>
                <!-- /.box-header -->

                <div class="box-body table-responsive no-padding">


                    <table class="table table-hover">
                        <tr>
                            <th class="col-sm-1" style="width: 50px">ID</th>
                            <th class="col-sm-1" style="width: 50px">商品id</th>
                            <th class="col-sm-1" style="width: 50px">用户id</th>
                            <th class="col-sm-5" >操作内容</th>
                            <th class="col-sm-2" style="width: 150px;">操作状态</th>
                            <th class="col-sm-2" style="width: 150px;">操作时间</th>

                        </tr>

                        <tr style="display:none;">
                            <td colspan="11">
                            {!! Form::open(['route' => ['admin.stock_history.destroy'],'name'=>'hid', 'method'=>'delete']) !!}

                            {!! Form::submit('删除',array('class' => 'btn btn-danger')) !!}

                            {!!Form::close()   !!}
                            </td>
                        </tr>
                        @foreach ($goodsStockHistory as $item)
                        <tr>
                            <td class="col-sm-1">{{$item->goods_stock_history_id}}</td>
                            <td class="col-sm-1">{{$item->goods_id}}</td>
                            <td class="col-sm-1">{{$item->user_id}}</td>
                            <td class="col-sm-5">{{$item->describe}}</td>
                            <td class="col-sm-2">
                                <?php
                                    switch($item->stock_status){
                                        case 1:
                                            echo "入库";
                                            break;
                                        case 2:
                                            echo "出库";
                                            break;
                                        case 3:
                                            echo "其他";
                                            break;
                                        default:
                                            echo "操作类型有误";
                                            break;
                                    }
                                ?></td>
                            <td class="col-sm-2">{{date('Y-m-d H:i:s',$item->add_time)}}</td>

                        </tr>
                        @endforeach

                    </table>

                </div>
                <!-- /.box-body -->

                <div class="box-footer">
                    <!-- 分页 -->
                    <div class="pull-right no-margin-pager">
                        {!! $goodsStockHistory->appends(['goods_id' => $search['goods_id'], 'stock_status' => $search['stock_status']])->render() !!}
                    </div>

                </div>

            </div>
            <!-- /.box -->

        </div>
    </div>


<!-- Modal -->
<div class="modal fade" id="warmModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close close_btn" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">信息提示</h4>
      </div>
      <div class="modal-body" id="errorMessage">
        
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
      </div>
    </div>
  </div>
</div>
</div>


<!-- /.content -->

<script>
var wind_status = '';
$(".close_btn").click(function(e){
    wind_status = 'close';
});

$(".toggle_sale").click(function(e){
    var that=$(this),checkbox=that.closest('tr').find('[type="checkbox"]');
    var id = $(e.currentTarget).attr('goods_id');

     $.ajax({ 
            type: "GET", 
            url: '/admin/goods/update_one/'+id, 
            data:'',
            dataType: "json", 
            headers: {
                'X-CSRF-Token': "{{csrf_token()}}"
            },
            async:true,
            cache:false,
            success: function (msg) { 

                    if(msg.is_on_sale==1){

                        that.removeClass('glyphicon-unchecked');
                        that.css("color","green");
                        that.addClass('glyphicon-check');
                        checkbox.prop('disabled',false);
                    }else{
                        that.css("color","red");

                        that.removeClass('glyphicon-check');

                        that.addClass('glyphicon-unchecked');
                        checkbox.prop('disabled',true);

                    }
            
            }
        }); //end ajax


});

var goodsArr =[]; 
$('#addAct').click(function(){
    goodsArr =[]; 

    $('input[name="selGoods"]:checked').each(function(){ 
        goodsArr.push($(this).val()); 
    });

    if(goodsArr.length==0){

        $('#warmModal').modal('show');
        $('#errorMessage').html('您还没有选择任何商品');

    }else{

        $('#actCat').modal('show');

    }//end if()
    
});

$('#addRecommend').click(function(){
    goodsArr =[]; 

    $('input[name="selGoods"]:checked').each(function(){ 
        goodsArr.push($(this).val()); 
    });

    if(goodsArr.length==0){

        $('#warmModal').modal('show');
        $('#errorMessage').html('您还没有选择任何商品');

    }else{

        $('#recommend').modal('show');

    }//end if()
    
});



$('#activity_id').click(function(){
    
    var activity_id = $("#activity_id option:selected").val();
    $("#activity_id").css("color","black");
    $("#act_cat_id").css("color","black");

    urls = "/admin/activity_cat/"+activity_id;
    
    $.ajax({ 
        type: "GET", 
        url: urls, 
        data:'',
        dataType: "json", 
        headers: {
            'X-CSRF-Token': "{{csrf_token()}}"
        },
        async:true,
        cache:false,
        success: function (msg) { 
            $('#act_cat_id').empty();
            
            if(msg.length==0){

                $('#act_cat_id').append('<option value="0">请选择分类</option>');
            }else{

                $.each(msg, function(n, value){
                    $('#act_cat_id').append('<option value="'+n+'">'+value+'</option>');
                });
            }//end if()
                   
        }
    }); //end ajax

});


$('#add_goods_to_activity').click(function(e){
    var dataForm = {};
    var activity_id = $('#activity_id').val();
    var act_cat_id = $("#act_cat_id").val();

    wind_status = 'add_activity';

    if(activity_id==null || activity_id=='' || activity_id==0 || act_cat_id==null || act_cat_id=='' || act_cat_id==0){


    }else{

        dataForm.goodsArr = goodsArr;

        urls = "/admin/addGoodsToAct/"+act_cat_id;
        
        $.ajax({ 
            type: "GET", 
            url: urls, 
            data: dataForm,
            dataType: "json", 
            headers: {
                'X-CSRF-Token': "{{csrf_token()}}"
            },
            async:true,
            cache:false,
            success: function (msg) { 
                
                console.log(msg);
                if(msg.status){
                    
                    $('#warmModal').modal('show');
                    $('#errorMessage').html('添加成功');

                }//end if()
                       
            }
        }); //end ajax
    }//end if()

});
   

$('#actCat').on('hidden.bs.modal', function () {

    var activity_id = $('#activity_id').val();
    var act_cat_id = $("#act_cat_id").val();

    if(wind_status == 'add_activity'){

        if(activity_id==null || activity_id=='' || activity_id==0){

            $("#activity_id").css("color","red");

            $("#activity_id option").each(function(){ //遍历全部option
                $(this).css("color","black");
                $("#activity_id option[value='0']").css("color","red");
            });

            $('#actCat').modal('show');

        }//end if()

        if(act_cat_id==null || act_cat_id=='' || act_cat_id==0){
            $("#act_cat_id").css("color","red");

            $("#activity_id option").each(function(){ //遍历全部option
                $(this).css("color","black");
                $("#activity_id option[value='0']").css("color","red");
            });

            $('#actCat').modal('show');
        }//end if()
    }//end if()
});


$(".is_home_recommend").click(function(e){

    var id = $(e.currentTarget).attr('goods_id');
    var is_home_recommend = $(e.currentTarget).attr('name');

    $.ajax({ 
        type: "GET", 
        url: '/admin/goods/'+id+'/'+is_home_recommend+'/update_two', 
        data:'',
        dataType: "json", 
        headers: {
            'X-CSRF-Token': "{{csrf_token()}}"
        },
        async:true,
        cache:false,
        success: function (msg) { 

                if(msg.is_home_recommend==1){

                    $(e.currentTarget).removeClass('glyphicon-unchecked');
                    $(e.currentTarget).css('color', 'green');

                    $(e.currentTarget).addClass('glyphicon-check');
                    
                }else{

                    $(e.currentTarget).removeClass('glyphicon-check');
                    $(e.currentTarget).css('color', 'red');

                    $(e.currentTarget).addClass('glyphicon-unchecked');

                }
        
        }
    }); //end ajax


});

$(".is_cat_recommend").click(function(e){

    var id = $(e.currentTarget).attr('goods_id');
    var is_cat_recommend = $(e.currentTarget).attr('name');

    $.ajax({ 
        type: "GET", 
        url: '/admin/goods/'+id+'/'+is_cat_recommend+'/update_two', 
        data:'',
        dataType: "json", 
        headers: {
            'X-CSRF-Token': "{{csrf_token()}}"
        },
        async:true,
        cache:false,
        success: function (msg) { 

                if(msg.is_cat_recommend==1){

                    $(e.currentTarget).removeClass('glyphicon-unchecked');
                    $(e.currentTarget).css('color', 'green');

                    $(e.currentTarget).addClass('glyphicon-check');
                    
                }else{

                    $(e.currentTarget).removeClass('glyphicon-check');
                    $(e.currentTarget).css('color', 'red');

                    $(e.currentTarget).addClass('glyphicon-unchecked');

                }
        
        }
    }); //end ajax


});



$('#add_goods_to_recommend').click(function(e){

    var dataForm = {};
    var recommend_id = $('#recommend_id').val();
    wind_status = 'add_recommend';

    if(recommend_id==null || recommend_id=='' || recommend_id==0 ){


        $("#recommend_id").css("color","red");

        $("#recommend_id option").each(function(){ //遍历全部option
            $(this).css("color","black");
            $("#recommend_id option[value='0']").css("color","red");
        });

        //$('#recommend').modal('show');

    }else{
        wind_status = 'close';

        dataForm.relationArr = goodsArr;

        urls = "/admin/add_goods_to_recommend/"+recommend_id;

        $.ajax({ 
            type: "GET", 
            url: urls, 
            data: dataForm,
            dataType: "json", 
            headers: {
                'X-CSRF-Token': "{{csrf_token()}}"
            },
            async:true,
            cache:false,
            success: function (msg) { 
                
                console.log(msg);
                if(msg.status){
                    
                    $('#warmModal').modal('show');
                    $('#errorMessage').html('添加成功');

                }//end if()
                       
            }
        }); //end ajax
    }//end if()
});


$('#recommend').on('hidden.bs.modal', function () {

    if(wind_status == 'add_recommend'){
        $('#recommend').modal('show');
    }//end if()
});


$('#recommend_id').click(function(){
        $("#recommend_id").css("color","black");
});

</script>
@endsection